package com.dw.widget.slideshow2.config.view.list;

import java.util.ArrayList;
import java.util.List;

import com.haarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class ListBlockView extends ListView{

	private ListBlockAdapter adapter;
	
	public ListBlockView(Context context) {
		
		this(context, null, -1);
	}

	public ListBlockView(Context context, AttributeSet attrs) {
		this(context, attrs, -1);
	}

	public ListBlockView(Context context, AttributeSet attrs, int defStyle) {
		
		super(context, attrs, defStyle);
		
		adapter = new ListBlockAdapter();
		
		this.setScrollingCacheEnabled(false);
		
		this.setFooterDividersEnabled(true);
		this.setDivider(new ColorDrawable(0x00FFFFFF));
		this.setDividerHeight(3);
		
		SwingBottomInAnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(adapter);
		animAdapter.setListView(this);
		setAdapter(animAdapter);
	}

	@Override
	protected void onFinishInflate() {

		super.onFinishInflate();
	}

	public final ListBlockAdapter getAdapter() {
		return adapter;
	}
	
	public void addListItem(BaseListItemView view)
	{
		adapter.add(view);
	}
	
	public void removeListItem(int pos)
	{
		adapter.remove(pos);
	}
	
	public List<BaseListItemView> getListItems()
	{
		return adapter.getListItems();
	}
	
	public void clear()
	{
		adapter.clear();
	}

	public class ListBlockAdapter extends BaseAdapter
	{

		private List<BaseListItemView> listItems;
		
		public List<BaseListItemView> getListItems()
		{
			return listItems;
		}
		
		public ListBlockAdapter()
		{
			listItems = new ArrayList<BaseListItemView>();
		}
		
		public void add(BaseListItemView view)
		{
			listItems.add(view);
			this.notifyDataSetChanged();
		}
		
		public void remove(int pos)
		{
			listItems.remove(pos);
			this.notifyDataSetChanged();
		}
		
		public void clear()
		{
			listItems.clear();
			this.notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {

			return listItems.size();
		}

		@Override
		public Object getItem(int position) {

			if(position < 0 || position >= listItems.size())
			{
				return null;
			}
			
			return listItems.get(position);
		}

		@Override
		public long getItemId(int position) {

			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if(position < 0 || position >= listItems.size())
			{
				return null;
			}
			
			BaseListItemView view  = listItems.get(position);
			
			// 为列表项增加背景
			if(view.ItemType == BaseListItemView.BLOCKHEADFOOTITEM)    // 只有一个列表项
			{
				
			}
			else if(view.ItemType == BaseListItemView.BLOCKHEADERITEM)
			{
				
			}
			else if(view.ItemType == BaseListItemView.BLOCKFOOTERITEM)
			{
				
			}
			else
			{
				
			}
			
			return view;
		}
		
	}
}
